// pages/SuSong/counsel/counsel.js
import request from '../../../utils/config.js';
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    custInfo:{},
    caseType:"0",
    caseTypeList: ['诉讼案件','执行案件'],
    assistMode:'0',
    assistModeList:['面谈','电话'],
    // caseTypeName:"",
    txtDate:"",
    // txtTime: new Date().getHours() >= 10 ? new Date().getHours() + ":00" : "0" + new Date().getHours() + ":00",
    txtTime:"",
    caseAmt:""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onShow: function () {
    this.queryDetail();
  },
  onLoad: function (options) {
    if (options.caseAmt) {
      this.setData({
        caseAmt: options.caseAmt
      });
    } else {
      this.setData({
        caseAmt: ''
      });
    }
    this.setData({ flag: options.flag})
  },

  queryDetail: function () {
    let _this = this;
    wx.showLoading({ title: '加载中' })
    request.requestMisApi('get', "custs/" + app.globalData.custId, {}, (res) => {
      let resInfo = JSON.parse(res.body)
      console.log("查询客户详情成功=", resInfo);
      resInfo.custVo.img = (resInfo.custVo.grade == 3) ? 'stateA.png' : (resInfo.custVo.grade == 2) ? 'stateB.png' : (resInfo.custVo.grade == 1) ? 'stateC.png' : 'state0.png';
      resInfo.talkAddress = resInfo.custVo.address;
      _this.setData({
        custInfo: resInfo
      });
      wx.hideLoading()
    }, (res) => {
      wx.hideLoading()
      console.log("查询客户详情失败=", res)
    });
  },
  //获取案件列表
  // getcaseTypes: function(){
    // let _this = this;
    // request.requestGetMis_api("caseTypes", wx.getStorageSync("token"), {}, (res) => {
    //   console.log("获取案件类型成功=", res)
    //   let caseTypeList = res.data;
    //   this.setData({
    //     caseTypeList: caseTypeList
    //   });
     
    // }, (res) => {
    //   console.log("获取案件类型失败=", res)
    // });
  // },

  // //触发案件类型选择
  // changeCaseType: function (e) {
  //   let value = e.detail.value;
  //   let typeList = this.data.caseTypeList;
  //   this.setData({
  //     caseType: value,
  //     caseTypeName: typeList[value].caseTypeName
  //   });
  // },

  //触发日期
  txtDateChange: function(e){
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      txtDate: e.detail.value
    })
  },

  //触发时间
  txtTimeChange: function(e){
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      txtTime: e.detail.value
    })
  },

  caseTypeChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      caseType: e.detail.value
    })
  },
  assistModeChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      assistMode: e.detail.value
    })
  },
  //触发金额
  bindAmt: function(e){
    // let val = e.detail.value;
    // if (isNaN(val*1)){
    //   this.setData({
    //     caseAmt: ""
    //   });
    // }else{
    //   this.setData({
    //     caseAmt: val
    //   });
    // }
    
  },


  //提交数据
  formSubmit: function(e){
    console.log('form发生了submit事件，携带数据为：', e.detail.value)
    let tempData = e.detail.value;
    let custId = this.data.custInfo.custVo.custId;
    if (tempData.caseType != 0 && tempData.caseType != 1) {
      wx.showToast({ title: '案件类型不能为空', icon: 'none', duration: 800 });
      return
    }
    // if (!tempData.txtDate || !tempData.txtTime) {
    //   wx.showToast({ title: '请选择时间', icon: 'none', duration: 800 });
    //   return
    // }
    let bodyInfo = {
      talkAddress: tempData.address,
      caseType: tempData.caseType*1+1,
      assistMode: tempData.assistMode * 1 + 1,
      creditorName: tempData.creditorName,
      debtorName: tempData.debtorName,
      caseAmt: tempData.amt,
      memo: tempData.memo,
      saleChanceId: this.data.custInfo.saleVo.saleChanceId
    };
    if (tempData.txtDate && tempData.txtTime){
      bodyInfo.talkTimeStr= tempData.txtDate + " " + tempData.txtTime + ":00"
    }
    console.log("bodyInfo===", bodyInfo)
    wx.showLoading({ title: '加载中' })
    let This=this;
    request.requestMisApi('post', "lawyerAssist/" + custId, bodyInfo, (res) => {
      console.log("提交律师协助成功=",res);
      wx.hideLoading();
      if (res.code == '00') {
        wx.showToast({
          title: '提交成功',
          icon: 'success',
          duration: 2000
        })
        if (This.data.flag =='second'){
          app.globalData.listRefresh.SuSong = {
            refresh: true
          }
        }
        setTimeout(function () {
          wx.navigateBack();
        }, 1000);
      } else {
        wx.showToast({
          title: res.msg,
          icon: 'none',
          duration: 2000
        })
      }
    }, (res) => {
      wx.hideLoading()
      console.log("提交律师协助失败=", res)
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  //拨打电话
  callPhone: function (e) {
    let pho = e.currentTarget.dataset.phone;
    wx.makePhoneCall({
      phoneNumber: pho
    })
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    wx.stopPullDownRefresh()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  // onShareAppMessage: function () {
  
  // }
})